package com.sgcc.operate.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.common.datascope.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.operate.domain.OSeedMng;
import com.sgcc.operate.service.IOSeedMngService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 种子用户管理Controller
 *
 * @author sgcc
 * @date 2021-03-27
 */
@RestController
@RequestMapping("/seedmng")
public class OSeedMngController extends BaseController {
    @Autowired
    private IOSeedMngService oSeedMngService;

    /**
     * 查询种子用户管理列表
     */
    //@PreAuthorize(hasPermi = "operate:seedmng:list")
    @GetMapping("/list")
    @DataScope(deptAlias = "o2")
    public TableDataInfo list(OSeedMng oSeedMng) {
        startPage();
        List<OSeedMng> list = oSeedMngService.selectOSeedMngList(oSeedMng);
        return getDataTable(list);
    }

    /**
     * 导出种子用户管理列表
     */
    //@PreAuthorize(hasPermi = "operate:seedmng:export")
    @Log(title = "种子用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @DataScope(deptAlias = "o2")
    public void export(HttpServletResponse response, OSeedMng oSeedMng) throws IOException {
        List<OSeedMng> list = oSeedMngService.selectOSeedMngList(oSeedMng);
        ExcelUtil<OSeedMng> util = new ExcelUtil<OSeedMng>(OSeedMng.class);
        util.exportExcel(response, list, "seedmng");
    }

    /**
     * 获取种子用户管理详细信息
     */
    //@PreAuthorize(hasPermi = "operate:seedmng:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(oSeedMngService.selectOSeedMngById(id));
    }

    /**
     * 新增种子用户管理
     */
    //@PreAuthorize(hasPermi = "operate:seedmng:add")
    @Log(title = "种子用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OSeedMng oSeedMng) {
        OSeedMng sele = new OSeedMng();
        sele.setCertnumber(oSeedMng.getCertnumber());
        List<OSeedMng> list = oSeedMngService.selectOSeedMngList(sele);
        if (list.size() != 0) {
            return AjaxResult.error("认证号码已存在");
        }
        return toAjax(oSeedMngService.insertOSeedMng(oSeedMng));
    }

    /**
     * 修改种子用户管理
     */
    //@PreAuthorize(hasPermi = "operate:seedmng:edit")
    @Log(title = "种子用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OSeedMng oSeedMng) {
        OSeedMng sele = new OSeedMng();
        sele.setCertnumber(oSeedMng.getCertnumber());
        List<OSeedMng> list = oSeedMngService.selectOSeedMngList(sele);
        if (list.size() != 0 && !list.get(0).getId().equals(oSeedMng.getId())) {
            return AjaxResult.error("认证号码已存在");
        }

        return toAjax(oSeedMngService.updateOSeedMng(oSeedMng));
    }

    /**
     * 删除种子用户管理
     */
    //@PreAuthorize(hasPermi = "operate:seedmng:remove")
    @Log(title = "种子用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(oSeedMngService.deleteOSeedMngByIds(ids));
    }
}
